package com.sevlet;


import com.Bean.Employee;
import com.dao.EmployeeDao;
import com.dao.impl.EmployeeDaoimpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JWTTokenUtil;
import com.util.JsonUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet  extends BaseSevlet{

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String referer = req.getHeader("Referer");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        PrintWriter out = resp.getWriter();
        JsonUtil jsonUtil = new JsonUtil();

        EmployeeDao employeeDao =(EmployeeDao) ProxyUtil.getDaoProxy(new EmployeeDaoimpl());
        if (referer!=null && referer.equals("http://localhost:8080/")){
            try {
                Employee employee = employeeDao.login(username, password);
                if (employee==null){
                    jsonUtil.setCode(1);
                    jsonUtil.setMsg("您的用户名或者密码错误");
                    out.write(gson.toJson(jsonUtil));
                }else {
                    JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();
                      String employeeJson  =  gson.toJson(employee);//把UserBean转换为 Json串
                    String jsonJwt =   jwtTokenUtil.getEncodeToken(employeeJson);
                    System.out.println("对用户做JWT加密后的结果为:"+jsonJwt);
                    jsonUtil.setCode(0);
                    jsonUtil.setMsg("登录成功");
                    jsonUtil.setData(jsonJwt);
                    out.print(gson.toJson(jsonUtil));
                }
            } catch (Exception e) {
                e.printStackTrace();
                jsonUtil.setCode(1);
                jsonUtil.setMsg("系统错误");
                out.write(gson.toJson(jsonUtil));
            }
        }else{
            jsonUtil.setCode(1);
            jsonUtil.setMsg("只能通过前端VUE项目访问该 Servlet实现用户登录");
            out.print(gson.toJson(jsonUtil));
        }
    }

    /**
     * 用手机号
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void mobilelogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //HttpServletRequest对象中，可以调用封装好的方法获得http协议的相关数据


        String mobildPhone = req.getParameter("mobildPhone");


        EmployeeDao employeeDao =(EmployeeDao) ProxyUtil.getDaoProxy(new EmployeeDaoimpl());

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        PrintWriter out = resp.getWriter();
        JsonUtil jsonUtil = new JsonUtil();

        try {
            Employee employee =  employeeDao.userLoginMobile(mobildPhone);

            if (employee!=null){
                JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();
                String employeeJson  =  gson.toJson(employee);//把UserBean转换为 Json串
                String jsonJwt =   jwtTokenUtil.getEncodeToken(employeeJson);
                jsonUtil.setCode(0);
                jsonUtil.setData(jsonJwt);
            }else{
                jsonUtil.setCode(1);
                jsonUtil.setMsg("登录失败，手机号不存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setData("系统错误:"+e.getMessage());
        }
        out.print(gson.toJson(jsonUtil));

    }
}
